/*
 * Copyright 2002-2006 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */
package pikes.xml;

import java.io.IOException;
import java.io.StringWriter;

import org.junit.Assert;
import org.junit.Test;

import pikes.xml.XMLMarkup;
import pikes.xml.XMLTag;
import pikes.xml.XMLVisitingException;
import pikes.xml.XMLVisitor;
import pikes.xml.XMLWriter;

public class TagContentTestCase {

	@Test
	public void shouldFailWhenCreatingWithNullParentTagName() throws Exception {
		try {
			new TagContent(null);
			Assert.fail();
		}
		catch (Exception e) {
			// left intentionally empty
		}

	}

	@Test
	public void shouldFailWhenCreatingWithEmptyParentTagName() throws Exception {
		try {
			new TagContent("  ");
			Assert.fail();
		}
		catch (Exception e) {
			// left intentionally empty
		}

	}

	@Test
	public void shouldPrintEmptyClosingTag() throws Exception {
		TagContent tag = new TagContent("name");
		StringWriter writer = new StringWriter();
		XMLVisitor xmlWriter = new XMLWriter(writer);
		tag.visit(xmlWriter);
		String actual = writer.toString();
		Assert.assertEquals("/>", actual);
	}

	@Test
	public void testPrint2() throws Exception {
		TagContent tag = new TagContent("name");
		tag.add(new XMLTag("child"));
		StringWriter writer = new StringWriter();
		XMLVisitor xmlWriter = new XMLWriter(writer);
		tag.visit(xmlWriter);
		String actual = writer.toString();
		Assert.assertEquals("><child/></name>", actual);
	}

	@Test
	public void testEquals() throws Exception {
		TagContent tag1 = new TagContent("name");
		TagContent tag2 = new TagContent("name1");
		Assert.assertTrue(tag1.equals(tag2));
	}

	@Test
	public void testDoesNotEqualsWithDifferentChildren() throws Exception {
		TagContent tag1 = new TagContent("name");
		tag1.add(new XMLMarkup() {

			public void visit(XMLVisitor xmlWriter) throws XMLVisitingException {
				// TODO Auto-generated method stub

			}

			@Override
			public boolean equals(Object obj) {
				return super.equals(obj);
			}

			public void print(Appendable appendable) throws IOException {
				// TODO Auto-generated method stub

			}
		});
		TagContent tag2 = new TagContent("name1");
		Assert.assertFalse(tag1.equals(tag2));
	}

	@Test
	public void testEqualsWithDifferentChildren() throws Exception {
		TagContent tag1 = new TagContent("name");
		tag1.add(new XMLMarkup() {

			public void visit(XMLVisitor xmlWriter) throws XMLVisitingException {
				// TODO Auto-generated method stub

			}

			@Override
			public boolean equals(Object obj) {
				return true;
			}

			public void print(Appendable appendable) throws IOException {
				// TODO Auto-generated method stub

			}
		});
		TagContent tag2 = new TagContent("name1");
		tag2.add(new XMLMarkup() {

			public void visit(XMLVisitor xmlWriter) throws XMLVisitingException {
				// TODO Auto-generated method stub

			}

			@Override
			public boolean equals(Object obj) {
				return super.equals(obj);
			}

			public void print(Appendable appendable) throws IOException {
				// TODO Auto-generated method stub

			}

		});
		Assert.assertTrue(tag1.equals(tag2));
	}
}
